package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool;

    //图片上传功能
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        //通过时间格式来设置文件的名称
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        Date date = new Date();
        String fileName1 = sdf.format(date);
        //获取到原始图片的名称
        String originalFilename = imgFile.getOriginalFilename();
        //截取到文件的后缀名称
        int index = originalFilename.lastIndexOf(".");
        String fileName =fileName1 + originalFilename.substring(index);
        //将上传文件的名称上传到Redis中
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        //通过工具类进行上传图片
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //添加操作
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setMealService.add(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        return setMealService.findByPage(queryPageBean);
    }

    //删除操作
    @RequestMapping("/deleteById")
    public Result deleteById(Integer setMealId){
        try {
            setMealService.deleteById(setMealId);
            return new Result(true,"删除套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除套餐失败");
        }
    }


    @RequestMapping("/findById")
    public Result findById(Integer setMealId){
        try {
            Setmeal setmeal = setMealService.findById(setMealId);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findByIdForCheckGroup")
    public Result findByIdForCheckGroup(Integer setMealId){
        try {
            List<Integer> list = setMealService.findByIdForCheckGroup(setMealId);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit (@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        //在修改数据之前根据id获取到数据库中的Img属性名称
        Setmeal oldSetMeal = setMealService.findById(setmeal.getId());
        String oldImg = oldSetMeal.getImg();

        try {
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,setmeal.getImg());
            setMealService.edit(setmeal,checkgroupIds);
            //如果修改操作成功删除对应的Redis缓存信息
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,oldImg);
            return new Result(true,"修改套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改套餐失败");
        }
    }

    //删除redis中的缓存信息
    @RequestMapping("/deleteRedisByUrl")
    public void deleteRedisByUrl(String imageUrl){
        int index = imageUrl.lastIndexOf("/");
        String oldFileName = imageUrl.substring(index);
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,oldFileName);
    }
}
